<?php
	/**
	 * 模板类
	 * Enter description here ...
	 * @author cui lixin
	 *
	 */
	class template extends base
	{
		/**
		 * 模块名
		 * Enter description here ...
		 * @var unknown_type
		 */
		private static $module_name;

		/**
		 * 模板名
		 * Enter description here ...
		 * @var unknown_type
		 */
		private static $template_name;

		/**
		 * 模板路径
		 * Enter description here ...
		 * @var unknown_type
		 */
		public static $template_path;

		/**
		 * 模板状态
		 * TRUE为呈现，FALSE为禁止呈现
		 * @var unknown_type
		 */
		private static $status = TRUE;

		/**
		 * 网页内容
		 * Enter description here ...
		 * @var unknown_type
		 */
		private static $content;
		/**
		 * 设置模板路径
		 * Enter description here ...
		 * @param unknown_type $data
		 */
		public static function set_template_path($data)
		{
			template_data::get_self()->path = 'module/' .
											  $data['module_name'] .
											  '/' .
											  config::get()->config->html_folder_name .
											  '/';

			self::$module_name   = $data['module_name'];
			self::$template_name = $data['controller_name'];

			self::merge_template_path();
		}

		/**
		 * 修改模板名称
		 * Enter description here ...
		 * @param unknown_type $file_name
		 */
		public static function edit_template_path($file_name)
		{
			self::$template_name = $file_name;

			self::merge_template_path();
		}

		/**
		 * 合成模板路径
		 * Enter description here ...
		 */
		private static function merge_template_path()
		{
			self::$template_path = MODULE_PATH .
								   self::$module_name .
								   DIRECTORY_SEPARATOR .
								   config::get()->config->html_folder_name .
								   DIRECTORY_SEPARATOR .
								   self::$template_name .
								   config::get()->config->html_ext;
		}

		/**
		 * 呈现页面
		 * Enter description here ...
		 * @param unknown_type $data
		 */
		public static function display($data)
		{
			if (self::$status === FALSE)
			{
				return FALSE;
			}

			self::get_template_content();
		}

		/**
		 * 获取模板内容
		 * Enter description here ...
		 */
		private static function get_template_content()
		{
			$file_path = file_cache::is_expired('html', self::$template_path, config::get()->cache->html_cache_time);

			if ($file_path)
			{
				self::include_template($file_path);
			}
			else
			{
				if (file_exists(self::$template_path))
				{
					self::$content = file_get_contents(self::$template_path);

					self::html_replace();

					file_cache::set('html', self::$template_path, self::$content);

					self::include_template(file_cache::is_expired('html', self::$template_path, config::get()->cache->html_cache_time));
				}
				else
				{
					throw_exception::process(new Exception('模板文件不存在：' . self::$template_path, 6));
				}
			}
		}

		/**
		 * 引入模板文件
		 * Enter description here ...
		 * @param unknown_type $file_path
		 */
		private static function include_template($file_path)
		{
		    $data = template_data::get_self();

			include $file_path;
		}

		/**
		 * 预处理HTML
		 * Enter description here ...
		 * @param unknown_type $content
		 */
		private static function html_replace()
		{
			self::rebuild_img_url();
			self::rebuild_js_url();
			self::rebuild_style_url();

			self::rebuild_label();
		}

		/**
		 * 替换标签
		 * Enter description here ...
		 */
		private static function rebuild_label()
		{
			self::$content = str_replace('<{dddd}>', "<?php echo 'I am 166 line!!!'; ?>", self::$content);
		}

		/**
		 * 替换图片路径
		 * 图片目录必须为image
		 */
		private static function rebuild_img_url()
	    {
	        preg_match_all('/(<img.*?src=)["\'](image[^"\']+)["\'](.*?>)/', self::$content, $matches);

	        if (empty($matches[0]))
	        {
	        	return;
	        }

	        $replace = array();

	        foreach ($matches[0] AS $v)
	        {
	        	$replace[] = preg_replace('/["\']image/', template_data::get_self()->path.'image', $v);
	        }

	        self::$content = str_replace($matches[0], $replace, self::$content);
	    }

		/**
		 * 替换JS路径
		 * 图片目录必须为image
		 */
		private static function rebuild_js_url()
	    {
	        preg_match_all('%<script.*?src=["\'](js[^"\']+)["\'].*?(utf-8["\'])?></script>\s*%i', self::$content, $matches);

	        if (empty($matches[0]))
	        {
	        	return;
	        }

	        $replace = array();

	        foreach ($matches[0] AS $v)
	        {
	        	$replace[] = preg_replace('/["\']js/', template_data::get_self()->path.'js', $v);
	        }

	        self::$content = str_replace($matches[0], $replace, self::$content);
	    }

		/**
		 * 替换CSS路径
		 * 图片目录必须为image
		 */
		private static function rebuild_style_url()
	    {
			preg_match_all('/<link.*?href="([^"]+\.css)".*?>\s*/', self::$content, $matches);

	        if (empty($matches[0]))
	        {
	        	return;
	        }

	        $replace = array();

	        foreach ($matches[0] AS $v)
	        {
	        	$replace[] = preg_replace('/"css/', '"' . template_data::get_self()->path.'css', $v);
	        }

	        self::$content = str_replace($matches[0], $replace, self::$content);
	    }

		/**
		 * 禁止呈现
		 * Enter description here ...
		 */
		public static function norand()
		{
			self::$status = FALSE;
		}
	}